package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysCompanyMember;
import com.ruoyi.system.service.ISysCompanyMemberService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 企业项目成员Controller
 * 
 * @author ruoyi
 * @date 2023-06-09
 */
@Api(tags = "企业项目成员")
@RestController
@RequestMapping("/system/member")
public class SysCompanyMemberController extends BaseController
{
    @Autowired
    private ISysCompanyMemberService sysCompanyMemberService;

    /**
     * 查询企业项目成员列表
     */
    @ApiOperation(value = "查询企业项目成员列表")
  //  @PreAuthorize("@ss.hasPermi('system:member:list')")
    @GetMapping("/list")
    public AjaxResult list(SysCompanyMember sysCompanyMember)
    {

        List<SysCompanyMember> list = sysCompanyMemberService.selectSysCompanyMemberList(sysCompanyMember);
        return AjaxResult.success(list);
    }

    /**
     * 导出企业项目成员列表
     */
    @ApiOperation(value = "导出企业项目成员列表")
  //  @PreAuthorize("@ss.hasPermi('system:member:export')")
    @Log(title = "企业项目成员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysCompanyMember sysCompanyMember)
    {
        List<SysCompanyMember> list = sysCompanyMemberService.selectSysCompanyMemberList(sysCompanyMember);
        ExcelUtil<SysCompanyMember> util = new ExcelUtil<SysCompanyMember>(SysCompanyMember.class);
        util.exportExcel(response, list, "企业项目成员数据");
    }

    /**
     * 获取企业项目成员详细信息
     */
    @ApiOperation(value = "获取企业项目成员详细信息")
 //   @PreAuthorize("@ss.hasPermi('system:member:query')")
    @GetMapping(value = "/{memberId}")
    public AjaxResult getInfo(@PathVariable("memberId") Long memberId)
    {
        return AjaxResult.success(sysCompanyMemberService.selectSysCompanyMemberByMemberId(memberId));
    }

    /**
     * 新增企业项目成员
     */
    @ApiOperation(value = "新增企业项目成员")
  //  @PreAuthorize("@ss.hasPermi('system:member:add')")
    @Log(title = "企业项目成员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysCompanyMember sysCompanyMember)
    {
        return toAjax(sysCompanyMemberService.insertSysCompanyMember(sysCompanyMember));
    }

    /**
     * 修改企业项目成员
     */
    @ApiOperation(value = "修改企业项目成员")
   // @PreAuthorize("@ss.hasPermi('system:member:edit')")
    @Log(title = "企业项目成员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysCompanyMember sysCompanyMember)
    {
        return toAjax(sysCompanyMemberService.updateSysCompanyMember(sysCompanyMember));
    }

    /**
     * 删除企业项目成员
     */
    @ApiOperation(value = "删除企业项目成员")
  //  @PreAuthorize("@ss.hasPermi('system:member:remove')")
    @Log(title = "企业项目成员", businessType = BusinessType.DELETE)
	@DeleteMapping("/{memberIds}")
    public AjaxResult remove(@PathVariable Long[] memberIds)
    {
        return toAjax(sysCompanyMemberService.deleteSysCompanyMemberByMemberIds(memberIds));
    }

    /**
     * 判断用户是否为该企业组长
     */
    @ApiOperation(value = "判断用户是否为该企业组长")
    //   @PreAuthorize("@ss.hasPermi('system:member:query')")
    @GetMapping(value = "/leader/{deptId}")
    public AjaxResult getIsLeader(@PathVariable("deptId") Long deptId)
    {
        Long userId=getLoginUser().getUserId();
        SysCompanyMember sysCompanyMember=new SysCompanyMember();
        sysCompanyMember.setMemberType("1");
        sysCompanyMember.setDeptId(deptId);
        sysCompanyMember.setUserId(userId);
        List<SysCompanyMember> list = sysCompanyMemberService.selectSysCompanyMemberList(sysCompanyMember);
        if(CollUtil.isNotEmpty(list)){
            return AjaxResult.success(true);
        }else{
            return AjaxResult.success(false);
        }

    }

    /**
     * 批量新增企业项目成员
     */
    @ApiOperation(value = "批量新增企业项目成员")
    //  @PreAuthorize("@ss.hasPermi('system:member:add')")
    @Log(title = "批量新增企业项目成员", businessType = BusinessType.INSERT)
    @PostMapping("/batch")
    public AjaxResult addList(@RequestBody List<SysCompanyMember> sysCompanyMembers)
    {
        return toAjax(sysCompanyMemberService.insertSysCompanyMemberList(sysCompanyMembers));
    }
}
